package com.dhcc.bpm.modules.metadata.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.metadata.entity.Metadata;
import com.dhcc.bpm.modules.metadata.mapper.MetadataMapper;
import com.dhcc.bpm.modules.metadata.mapper.StrucMetaMapper;
import com.dhcc.bpm.modules.metadata.service.IMetadataService;
import com.dhcc.bpm.modules.metadata.vo.MetadataVO;
import com.dhcc.bpm.modules.metadata.wrapper.ApiMetadataWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MetadataServiceImpl
 * @Description 元数据表 服务实现类
 * @Author daina
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */
@Service("opc_metadata")
public class MetadataServiceImpl extends BaseServiceImpl<MetadataMapper, Metadata> implements IMetadataService {

    @Autowired
    private MetadataMapper metadataMapper;
    @Autowired
    private StrucMetaMapper strucMetaMapper;

    @Override
    public IPage<MetadataVO> selectMetadataPage(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(baseMapper.selectMetadataPage(page, metadata));
    }

    @Override
    public IPage<MetadataVO> structMetalist(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(baseMapper.structMetalist(page, metadata));
    }

    @Override
    public IPage<MetadataVO> selectGateMetadataList(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(metadataMapper.selectGateMetadataList(page, metadata));
    }

    @Override
    public IPage<MetadataVO> selectReqMetadataList(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(metadataMapper.selectReqMetadataList(page, metadata));
    }

    @Override
    public IPage<MetadataVO> selectResMetadataList(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(metadataMapper.selectResMetadataList(page, metadata));
    }

    @Override
    public IPage<MetadataVO> listUnSelectReqMetadata(IPage<MetadataVO> page, MetadataVO metadata) {
        List<MetadataVO> lists = metadataMapper.listUnSelectReqMetadata(page, metadata);
        // 元素类型 1 数据元素 2 复合元素 3 结构体
        // 结构体 组装层级关系数据
        if (null != metadata.getElementType() && "3".equals(metadata.getElementType())) {
            List<MetadataVO> endLists = new ArrayList<>();
            for (MetadataVO vo : lists) {
                vo.setParentId(0L);
                endLists.add(vo);
                Long metadataId = vo.getId();
                MetadataVO strucMeta = new MetadataVO();
                strucMeta.setStructureId(metadataId.toString());
                List<MetadataVO> strucMetaLists = strucMetaMapper.listStructData(strucMeta);
                for (MetadataVO metaChildren : strucMetaLists) {
                    metaChildren.setParentId(vo.getId());
                    metaChildren.setDisabled(true);
                    endLists.add(metaChildren);
                }
            }
            return page.setRecords(ApiMetadataWrapper.build().listNodeStruct(endLists));
        }else{
            return page.setRecords(lists);
        }
    }

    @Override
    public IPage<MetadataVO> listUnSelectStructMeta(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(metadataMapper.listUnSelectStructMeta(page, metadata));
    }

    @Override
    public IPage<MetadataVO> listUnSelectResMetadata(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(metadataMapper.listUnSelectResMetadata(page, metadata));
    }

    @Override
    public IPage<MetadataVO> listUnSelectCommonMetadata(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(metadataMapper.listUnSelectCommonMetadata(page, metadata));
    }

    @Override
    public MetadataVO selectDetailInfo(MetadataVO metadata) {
        return metadataMapper.selectDetailInfo(metadata);
    }

    @Override
    public List<Metadata> listMetadataToExcel(Metadata metadata) {
        return metadataMapper.listMetadataToExcel(metadata);
    }

    @Override
    public List<MetadataVO> tree() {
        return ForestNodeMerger.merge(baseMapper.tree());
    }

    @Override
    public IPage<MetadataVO> selectGateParamCheckList(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(baseMapper.selectGateParamCheckList(page, metadata));
    }

    @Override
    public IPage<MetadataVO> selectNotInGateParamCheckList(IPage<MetadataVO> page, MetadataVO metadata) {
        return page.setRecords(baseMapper.selectNotInGateParamCheckList(page, metadata));
    }

    @Override
    public IPage<MetadataVO> selectMetadataList(IPage<MetadataVO> page, Map<String, Object> metadata) {
        return page.setRecords(metadataMapper.selectMetadataList(metadata));
    }

    @Override
    public List<Metadata> selectMetadataListByCode() {
        return metadataMapper.selectMetadataListByCode();
    }

}
